﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Extractor.LibCode
{
    public enum SITE_MAPPING
    {
        SCORESWAY,
        SKYSPORT,
        ESPN,
        YAHOO,
        EPL,
        GOAL
    }
    public  class cMapping : cDBService
    {
        private const string MAPPING_TABLE_NAME = "TBMAPPING";
     

        public enum MAPPING_TYPE
        {
            PLAYERID,
            MATCHID,
            VENUEID,
            TEAMID,
            COMPETITIONID,
            SESSIONID,
            ROUNDID

        }
        //private string getMappingType(MAPPING_TYPE MT)
        //{
        //    if(MT==MAPPING_TYPE.PLAYERID
        //}

        public  string getFieldMapping(SITE_MAPPING site)
        {
            if (site == SITE_MAPPING.SCORESWAY)
            {
                return "SCORESWAYID";
            }
            else if (site == SITE_MAPPING.SKYSPORT)
            {
                return "SKYSPORTID";
            }
            else
            {
                return "";
            }
        }
        public bool CreateNewMapping(MAPPING_TYPE MAPTYPE, SITE_MAPPING WEBSITE, string LOCAL_CODE, string siteID,ref string localid)
        {
            if (CreateNewMapping(MAPTYPE, WEBSITE, LOCAL_CODE, siteID))
            {
                localid= getLocalCode(MAPTYPE, WEBSITE, siteID);
                return true;
            }
            return false;

        }

        public  bool CreateNewMapping(MAPPING_TYPE MAPTYPE, SITE_MAPPING WEBSITE, string LOCAL_CODE, string siteID)
        {
            string slocalcode = "";

            if (LOCAL_CODE == null)
                LOCAL_CODE = string.Empty;
            if (LOCAL_CODE.ToString().Trim() == string.Empty)
            {
                slocalcode = new cDBHelper().genIDCommit(MAPTYPE.ToString());
            }
            else
            {
                slocalcode = LOCAL_CODE;
                return true;
            }
            string sql = string.Format("insert into {0}(MAPPINGTYPE,LOCALID,{1}) VALUES ('{2}','{3}','{4}')",
                                        MAPPING_TABLE_NAME, getFieldMapping(WEBSITE), MAPTYPE.ToString(), slocalcode, siteID);


            try
            {
                if (Dbserver.openConn())
                {
                    bool bret = Dbserver.ExecuteNonQuery(sql);
                    Dbserver.CommitAndCloseConn();
                    return bret;
                   
                }
            }
            catch (Exception ex)
            {

                Dbserver.ForceRollBackNClose();
            }
            return false;


        }
        public string getLocalCode(MAPPING_TYPE MAPTYPE, SITE_MAPPING WEBSITE, string SITE_CODE)
        {

            string sql = string.Format("select localid from {3} where mappingtype='{0}'  and {1}='{2}'",
                        MAPTYPE.ToString(), getFieldMapping(WEBSITE), SITE_CODE, MAPPING_TABLE_NAME);
            return Dbserver.ExecuteScalar(sql).ToString().Trim();
        }

        public bool checkLocalCode(MAPPING_TYPE MAPTYPE, SITE_MAPPING WEBSITE, string SITE_CODE)
        {
            return getLocalCode(MAPTYPE, WEBSITE, SITE_CODE).Trim() != string.Empty;
        }

        public string getLocalCodeAndCreate(MAPPING_TYPE MAPTYPE, SITE_MAPPING WEBSITE, string SITE_CODE)
        {

            if (!checkLocalCode(MAPTYPE,WEBSITE,SITE_CODE))
            {
                if (CreateNewMapping(MAPTYPE, WEBSITE, "",SITE_CODE))
                {
                    return getLocalCode(MAPTYPE, WEBSITE, SITE_CODE);
                }
                else
                {
                    return "";
                }
                

            }
            else
            {
                return getLocalCode(MAPTYPE, WEBSITE, SITE_CODE);
            }

        }


    }
}
